package br.foton.la.agenda.dao;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

import javax.sql.DataSource;

import org.hibernate.Session;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import br.foton.la.agenda.entity.Pessoa;
import br.foton.la.agenda.interfaces.DAO;
import br.foton.la.agenda.util.HibernateUtil;

@Repository
public class PessoaDAO implements DAO<Pessoa> {
	private final Connection connection;

	@Autowired
	public PessoaDAO(DataSource dataSource) {
		try {
			this.connection = dataSource.getConnection();
		} catch (SQLException e) {
			throw new RuntimeException(e);
		}
	}

	@Override
	public void insere(Pessoa entity) {
		// TODO Auto-generated method stub
	}

	@Override
	public List<Pessoa> list() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Pessoa findById(Integer id) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void update(Pessoa entity) {
		// TODO Auto-generated method stub
	}

	@Override
	public void remove(Pessoa entity) {
		// TODO Auto-generated method stub
	}

	public Pessoa findByLogin(String login, String senha) {
		Session s = HibernateUtil.getSession();
		s.beginTransaction();
		return (Pessoa) s.createCriteria(Pessoa.class)
				.add(Restrictions.eq("login", login))
				.add(Restrictions.eq("senha", senha)).uniqueResult();
	}

	public Connection getConnection() {
		return connection;
	}

}
